<template>
  <div class="chat-app">
    <div class="chat-main" :class="{ 'expanded': sidebarCollapsed }">
      <ChatWindow
          v-if="activeFriend"
          :friend="activeFriend"
          :collapsed="sidebarCollapsed"
      />
      <div v-else class="no-chat-selected">
        <div class="welcome-message">
          <h2>欢迎使用迅联平台</h2>
          <p>请从右侧选择好友开始聊天</p>
        </div>
      </div>
    </div>

    <FriendSidebar
        :collapsed="sidebarCollapsed"
        @toggle="toggleSidebar"
        @select-friend="selectFriend"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import FriendSidebar from './FriendSidebar.vue'
import ChatWindow from './ChatWindow.vue'

const sidebarCollapsed = ref(false)
const activeFriend = ref(null)

const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

const selectFriend = (friend) => {
  activeFriend.value = friend
}

onMounted(() => {
  const checkScreenSize = () => {
    if (window.innerWidth < 768) {
      sidebarCollapsed.value = true
    }
  }
  window.addEventListener('resize', checkScreenSize)
  checkScreenSize()
})
</script>

<style scoped>
.chat-app {
  display: flex;
  height: 89vh;
  overflow: hidden;
}

.chat-main {
  flex: 1;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
}

.expanded {
  margin-right: -200px;
}

.no-chat-selected {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  color: #666;
}

.welcome-message {
  text-align: center;
  padding: 20px;
}

.welcome-message h2 {
  margin-bottom: 10px;
  color: #2c3e50;
}

@media (max-width: 768px) {
  .expanded {
    margin-right: 0;
  }

  .chat-app {
    flex-direction: column-reverse;
  }

  .chat-main {
    margin-right: 0 !important;
  }
}
</style>